//
//  DownLoadViewController.m
//  FitTime
//
//  Created by 赵建平 on 16/4/9.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "DownLoadViewController.h"
#import "DownLoadTableViewCell.h"
#import "CoreDataManager.h"
#import "Single.h"
#import "DownMp4ViewController.h"

@interface DownLoadViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) CoreDataManager *manager;
@property (nonatomic, strong) NSMutableArray *mutableArray;



@end

@implementation DownLoadViewController

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    self.manager = [CoreDataManager shareCoreDataManager];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Single" inManagedObjectContext:self.manager.managedObjectContext];
    [request setEntity:entity];
    
    NSError *error = nil;
    
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    if (fetchedObjects == nil) {
        NSLog(@"%@", error);
    } else {
        
        self.mutableArray = [NSMutableArray arrayWithArray:fetchedObjects];
    }
    
    [self createTableView];
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[DownLoadTableViewCell class] forCellReuseIdentifier:@"DownLoadTableViewCell"];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.mutableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DownLoadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DownLoadTableViewCell"];
    
    Single *sing = self.mutableArray[indexPath.row];
    
    NSString *string = [NSString stringWithFormat:@"http://ft-video.fit-time.cn/%@.png@!medium", sing.singid];
    
    [cell.reftImageView sd_setImageWithURL:[NSURL URLWithString:string]];
    cell.titleLable.text = sing.title;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Single *sing = self.mutableArray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"%@", sing.title];
    
    DownMp4ViewController *down = [[DownMp4ViewController alloc] init];
    down.urlString = string;
    [self.navigationController pushViewController:down animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
